/*
 * @Author: TC
 * @Date: 2025-06-20 16:02:19
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-28 17:16:32
 */

import { app } from "db://assets/FrameWork/App";
import { BulletBase } from "../../../Game/Battle/Bullet/BulletBase";
import { EntityBase } from "../../../Game/Entity/EntityBase";
import { nsSkillConfig } from "../../../Config/SkillConfig";
import { nsBulletInterface } from "../../../Game/Battle/Bullet/BulletInterface";
import { nsBundleConst } from "../../../Const/BundleConst";
import { myGame } from "../../../MyGame";
import { nsBulletConst } from "../../../Const/BulletConst";
import { instantiate, Node, v3, Vec3 } from "cc";

/**
 * 子弹助手
 */
export namespace nsBulletHelper {
    /**
     * 创建子弹
     * @param cfgSkill
     * @param parent
     */
    export function createBullet(cfgSkill: CfgInterface.ISkill, parent: EntityBase | BulletBase): void {
        const cfgBullet = nsSkillConfig.getBulletCfg(cfgSkill.bulletId);
        //数量
        const num = cfgBullet.count;
        for (let i = 0; i < num; i++) {
            //加入到路径逻辑
            if (nsBulletConst.SHOOT_TYPE.DELAY == cfgBullet.shootType[0]) {
                const delay = cfgBullet.shootType[1] * i;
                app.timerMgr.scheduleOnce(
                    () => {
                        doCreateBullet(cfgSkill, cfgBullet, parent);
                    },
                    delay,
                    this
                );
            } else {
                doCreateBullet(cfgSkill, cfgBullet, parent);
            }
        }
    }

    function doCreateBullet(
        cfgSkill: CfgInterface.ISkill,
        cfgBullet: CfgInterface.IBullet,
        parent: EntityBase | BulletBase
    ): void {
        app.resMgr.loadPrefab(nsBundleConst.Skill, `${cfgSkill.bulletId}/Entity`, (p) => {
            const data: nsBulletInterface.IBullet = {
                cfg: cfgBullet,
            };
            const nodeBullet = app.poolMgr.getItem(cfgSkill.bulletId + "", p, data);
            // const nodeBullet = instantiate(p);
            // nodeBullet.getComponent(BulletBase).setData(data);
            // nodeBullet.getComponent(BulletBase)._cfg = cfgBullet;
            // app.tickMgr.add(nodeBullet.getComponent(BulletBase));
            const bullet = nodeBullet.getComponent(BulletBase);
            const iPath: nsBulletInterface.IBulletPath = {
                nodeBullet: nodeBullet,
                initTartetPos: _targetPos,
                bulletMove: bullet.move,
                bullet: bullet,
                index: 0,
            };
            _setBulletCreatePos(nodeBullet, cfgSkill, parent);
            //设置初始目标位置
            _setBulletTargetPos(cfgBullet, parent);
            myGame.battleMgr.motionLogic.addBullet(iPath);
        });
    }

    const createPos: Vec3 = v3();
    /**
     * 设置子弹创建位置
     * @param nodeBullet
     * @param cfgSkill
     * @param parent
     */
    function _setBulletCreatePos(nodeBullet: Node, cfgSkill: CfgInterface.ISkill, parent: EntityBase | BulletBase): void {
        switch (cfgSkill.postion) {
            case nsBulletConst.ECreatePos.PARENT_CAST_POS:
                createPos.set(parent.getCastPos());
                break;
            case nsBulletConst.ECreatePos.PARENT_NODE_POS:
                createPos.set(parent.getCastPos());
                break;
            case nsBulletConst.ECreatePos.NEAST_ENEMY_POS:
                createPos.set(parent.getCastPos());
                break;
            default:
                createPos.set(parent.getCastPos());
                break;
        }
        nodeBullet.setPosition(createPos);
    }

    /**
     * 获取子弹目标坐标
     * @param cfgBullet
     * @param parent
     * @param needDeftPos
     * @returns
     */
    const _targetPos: Vec3 = v3(0, 0, 0);
    function _setBulletTargetPos(cfgBullet: CfgInterface.IBullet, parent: EntityBase | BulletBase) {
        switch (cfgBullet.dir) {
            case nsBulletConst.EInitDir.FRONT: //前方
                _targetPos.set(parent.getCastPos().x, parent.getCastPos().y + 100);
                break;
            case nsBulletConst.EInitDir.NEAST_ENEMY: //最近敌人
                _targetPos.set(parent.node.position.x, parent.node.y + 100);
                break;
            case nsBulletConst.EInitDir.PARENT_BULLET_DIR: //父子弹方向
                _targetPos.set(parent.node.position.x, parent.node.y + 100);
                break;
            default:
                _targetPos.set(parent.node.position.x, parent.node.y + 100);
                break;
        }
    }
}
